/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.engine.alg;


import axescon.xacml.api.CtxHandler;
import axescon.xacml.api.XacmlException;
import axescon.xacml.api.alg.PolicyCombAlg;
import axescon.xacml.engine.PolicyEvaluatorImpl;
import axescon.xacml.model.ctx.Result;
import axescon.xacml.model.ctx.Status;
import axescon.xacml.model.ctx.XacmlCtx;
import axescon.xacml.model.ctx.impl.ResultImpl;
import axescon.xacml.model.policy.Obligation;
import axescon.xacml.model.policy.PolicyCombinerParameters;
import axescon.xacml.model.policy.PolicyProxy;
import axescon.xacml.model.shared.Decision;

import java.util.ArrayList;
import java.util.List;


/**
 * @author argyn
 *         Date: Jun 27, 2005
 *         Time: 11:09:19 PM
 *         Responsibilities: represents XACML Permit-overrides Policy Combining algorithm
 *         see spec 2.0 ch. C.3 and C.4
 *         id: urn:oasis:names:tc:xacml:1.0:policy-combining-algorithm:permit-overrides
 *         id: urn:oasis:names:tc:xacml:1.1:policy-combining-algorithm:ordered-permit-overrides
 */
public class OrderedPermitOverridesPolicyAlg implements PolicyCombAlg {


    public OrderedPermitOverridesPolicyAlg() {
    }


    /**
     * almost literal implementation of pseudo-code
     *
     * @param policies
     * @param req
     * @param ch
     * @param params
     * @return
     */
    public Result combine(PolicyProxy[] policies, XacmlCtx req, CtxHandler ch,
                          PolicyCombinerParameters[] params, PolicyEvaluatorImpl pe) throws XacmlException {
        boolean atLeastOneError = false;
        boolean atLeastOneDeny = false;
        //              List<Obligation>
        List obligations = new ArrayList();
        Status origErrorStatus = null;

        for (int i = 0; i < policies.length; i++) {
            PolicyProxy policy = policies[i];

            Result res = policy.eval(pe, req, ch);

            byte decision = res.getDecision();
            if (decision == Decision.BYTE_DENY) {
                atLeastOneDeny = true;
                if (res.getObligationList() != null) obligations.addAll(res.getObligationList());
                continue;
            }
            if (decision == Decision.BYTE_PERMIT) {
                return res;
            }
            if (decision == Decision.BYTE_NOT_APPLICABLE) {
                continue;
            }
            if (decision == Decision.BYTE_INDETERMINATE) {
                atLeastOneError = true;
                origErrorStatus = res.getStatus();
                continue;
            }
        }
        if (atLeastOneDeny) {

            ResultImpl res = new ResultImpl();
            res.setDecision(Decision.BYTE_DENY);
            res.setResourceId(ch.getResourceId(req));
            res.setObligationArray((Obligation[]) obligations.toArray(new Obligation[obligations.size()]));
            return res;
        }
        if (atLeastOneError) {
            ResultImpl res = new ResultImpl();
            res.setDecision(Decision.BYTE_INDETERMINATE);
            res.setResourceId(ch.getResourceId(req));
            res.setStatus(origErrorStatus);
            return res;
        }

        ResultImpl res = new ResultImpl();
        res.setDecision(Decision.BYTE_NOT_APPLICABLE);
        res.setResourceId(ch.getResourceId(req));
        return res;

    }

}
